package DataBase;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import ClasesBasicas.EstadoTren;
import ClasesBasicas.Tren;
import ClasesBasicas.Usuario;

public class TrenJDBC implements IDataBase{
	public static final String DRIVER_CLASS_NAME = "org.sqlite.JDBC";

	//public static final String CONNECTION_URL = "jdbc:sqlite:C:/redes.workspace/proyecto/trenes.s3db";
	public static final String CONNECTION_URL = "jdbc:sqlite:C:/Users/Raquel/workspaceRedes/trenes/src/Trenes.s3db";
	public static Connection connection;
	
	public TrenJDBC() throws ClassNotFoundException, SQLException{
		Class.forName(DRIVER_CLASS_NAME);
		connection= DriverManager.getConnection(CONNECTION_URL);
	}
	//FUNCIONA
	@Override
	public int insert(Object o) throws IllegalArgumentException,
			IllegalAccessException, SQLException {
		if(o instanceof Tren){
			Tren t= (Tren) o;
			Statement statement = null;
			statement =  connection.createStatement();
			String sql;
			int estadoGps;
			if (t.getEstadoGps()){
				estadoGps=1;
			}
			else
				estadoGps=0;
			
			sql = "INSERT INTO Tren(id,estado,gps)VALUES('"+t.getId()+"','"+t.getEstado()+"',"+estadoGps+");";
			System.out.println(sql);
			int filasInsertadas=statement.executeUpdate(sql);
			statement.close();
			
			return filasInsertadas;
		}
		else
			return 0;
	}
	//FUNCIONA PERO SI METES UN ID K NO EXISTE NO SLTA EXCEPCION
	@Override
	public int remove(Object o)  {
		if(o instanceof Tren){
			Tren t= (Tren) o;
		
		String sql = "DELETE FROM Tren WHERE id='"+t.getId()+"';";
		System.out.println(sql);
		int filasBorradas=0;
		try {
			Statement statement = connection.createStatement();
			filasBorradas = statement.executeUpdate(sql);
			statement.close();
			System.out.println("holaaa");
		} catch (SQLException e) {
			System.out.println("no existe dicho tren");
		}
		
		
		return filasBorradas;}
		
		else
			return 0;
	}
	//FUNCIONA
	@Override
	public Tren getByPrimaryKey(Object o) throws SQLException {
		if( o instanceof String){
		String sqlStatementString = null;
		Statement statement = null;
		ResultSet resultSet = null;
		statement = connection.createStatement();
		sqlStatementString = "SELECT * FROM Tren WHERE id='"+o+"';";
		System.out.println(sqlStatementString);
		Tren tren= new Tren();
		resultSet = statement.executeQuery(sqlStatementString);
		boolean gps;
		while(resultSet.next())
		{	
			tren.setId(resultSet.getString("id"));
			String estado= resultSet.getString("estado");
			
			if(estado.equals("Mantenimiento")){
				tren.setEstado(EstadoTren.Mantenimiento);	
			}
			else {
				if(estado.equals("Parado")){
					tren.setEstado(EstadoTren.Parado);
				}
				else
					tren.setEstado(EstadoTren.Funcionando);
			}
			
			
			tren.setEstadoGps(resultSet.getBoolean("gps"));
			

		}
		System.out.println(tren.getEstadoGps());
		System.out.println(tren.getId());
		System.out.println(tren.getEstado());
		resultSet.close();
		statement.close();
		return tren;}
		else
			return null;
	}
	@Override
	public ArrayList<Tren> getAll() throws SQLException {
		String sqlStatementString = null;
		Statement statement = null;
		ResultSet resultSet = null;
		statement = connection.createStatement();
		sqlStatementString = "SELECT * FROM Tren ;";
		System.out.println(sqlStatementString);
		ArrayList<Tren> trenes= new ArrayList<Tren>();
		resultSet = statement.executeQuery(sqlStatementString);
		while(resultSet.next())
		{	
			Tren t= new Tren();
			t.setId(resultSet.getString("id"));
			String estado= resultSet.getString("estado");
			
			if(estado.equals("Mantenimiento")){
				t.setEstado(EstadoTren.Mantenimiento);	
			}
			else {
				if(estado.equals("Parado")){
					t.setEstado(EstadoTren.Parado);
				}
				else
					t.setEstado(EstadoTren.Funcionando);
			}
			
			
			t.setEstadoGps(resultSet.getBoolean("gps"));
			trenes.add(t);

		}
		for(int i=0; i<trenes.size();i++){
			System.out.println(trenes.get(i).getEstadoGps());
			System.out.println(trenes.get(i).getId());
			System.out.println(trenes.get(i).getEstado());
			System.out.println("otro tren");
			
		}
		resultSet.close();
		statement.close();
		return trenes;
	}
	
	public void update(String idTren,boolean estado) throws SQLException{
		int e;
		if(estado){
			e=1;
			}
		else
			e=0;
		String sql = "UPDATE Tren SET gps ="+e+"  WHERE id = '"+idTren+"';";
		System.out.println(sql);
		
			Statement statement = connection.createStatement();
			int filasModificadas = statement.executeUpdate(sql);
			statement.close();
	
		
	}
	
	
}
